﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PK3Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace OneLine
{
    class Player
    {
        private List<Tower> towers = new List<Tower>();

        public Player()
        {
        }

        /*public void Start(Map map, ContentManager content)
        {
            for (int xx = 0; xx < map.Width; xx++)
            {
                for (int zz = 0; zz < map.Height; zz++)
                {
                    if (map.GetIndex(xx, zz) > 1)
                    {
                        AddTower(xx, zz, content);
                    }
                }
            }
        }*/

        public void AddTower(int xx, int zz, GraphicsDevice graphicsDevice, ContentManager content, string cmodel)
        {
            CPK3Model model = content.Load<CPK3Model>(cmodel);
            model.initialize(graphicsDevice);
            model.setAnimation("TORSO_STAND");
            model.setAnimation("LEGS_IDLE");
            Random rand = new Random();
            int id = rand.Next(0, 3);
            model.currentHeadID = id;
            model.currentLegsID = id;
            model.currentUpperID = id;

            Tower tower = new Tower(model) { Position = new Vector3(xx + 0.5f, 0.0f, zz + 0.5f) };
            towers.Add(tower);
        }

        public void Draw(GraphicsDevice graphicsDevice, Matrix view, Matrix projection)
        {
            foreach (Tower tower in towers)
                tower.Model.render(graphicsDevice,
                   Matrix.CreateRotationX((3.1415f / 2.0f) * 3.0f) * tower.Rotation * Matrix.CreateRotationY(MathHelper.ToRadians(-90)) *
                   Matrix.CreateScale(0.01f) *
                    Matrix.CreateTranslation(0, 0.3f, 0) * Matrix.CreateTranslation(tower.Position),
                   view, projection);
        }

        public void Update(Wave wave, GameTime gameTime)
        {
            foreach (Tower tower in towers)
                tower.Update(wave, gameTime);
        }
    }
}
